<? ?>
<script type="text/javascript">
	$(function() {
		
		$('#mySubmit,#mySubmit2').click(function() {
			if (!checkQty()) {
				return false;
			}
		})
		/*
		$('.bodyinput').each(function(index, obj) {
			var $this = $(this);
			var maxKAR = $this.attr("data-max-KAR");
			var maxPAK = $this.attr("data-max-PAK");
			var maxEA = $this.attr("data-max-EA");
			$this.find('input[name="addKAR[]"]:eq(0)').val(maxKAR);
			$this.find('input[name="addPAK[]"]:eq(0)').val(maxPAK);
			$this.find('input[name="addEA[]"]:eq(0)').val(maxEA);
			
		});*/
		//$('#mySubmit,#mySubmit2').click();
	})
	function checkQty() {
		var process = true;
		
		
		
		$('.bodyinput').each(function(index, obj) {
			var $this = $(this);
			var maxKAR = $this.attr("data-max-KAR");
			var maxPAK = $this.attr("data-max-PAK");
			var maxEA = $this.attr("data-max-EA");
			var pName = $this.attr("data-name");
			if (maxKAR != undefined) {
				var sum1 = 0;
				$.each($this.find('input[name="addKAR[]"]'), function(index2, obj2) {
					sum1+= parseInt($(this).val());
				});
				if(sum1!=maxKAR){
					alert("จำนวนของแถม รายการของ "+pName+ " ไม่ถูกต้อง");
					process = false;
					return false;
				}
			}
			if (maxPAK != undefined) {
				var sum1 = 0;
				$.each($this.find('input[name="addPAK[]"]'), function(index2, obj2) {
					sum1+= parseInt($(this).val());
				});
				if(sum1!=maxPAK){
					alert("จำนวนของแถม รายการของ "+pName+ " ไม่ถูกต้อง");
					process = false;
					return false;
				}
			}
			if (maxEA != undefined) {
				var sum1 = 0;
				$.each($this.find('input[name="addEA[]"]'), function(index2, obj2) {
					sum1+= parseInt($(this).val());
				});
				if(sum1!=maxEA){
					alert("จำนวนของแถม รายการของ "+pName+ " ไม่ถูกต้อง");
					process = false;
					return false;
				}
			}
		})
		return process;
	}
</script>
<form action="<?php echo $current?>tosummaryorder" method="post">
	<button id="mySubmit2" type="submit">ยืนยันการสร้างเอกสาร</button>
<input type="hidden" name="code" value="yes"/><h2 style="font-size:16px;font-weight:bold;">(<?php echo $customer->KUNNR?>)<?php echo $customer->NAME?></h2>
<table style="width:600px" class="myTable norborder" >
	<tr>
		<td style="width:30%">ที่อยู่</td>
		<td style="width:70%"><?php echo $customer->ADDRESS?></td>
	</tr>
	<tr>
		<td style="width:30%">โทรศัพท์</td>
		<td style="width:70%"><?php echo $customer->TELEPHONE?></td>
	</tr>
	<tr>
		<td style="width:30%">โทรสาร</td>
		<td style="width:70%"><?php echo $customer->FAX?></td>
	</tr>
	<tr>
		<td style="width:30%">อีเมล์</td>
		<td style="width:70%"><?php echo $customer->EMAIL?></td>
	</tr>
	<tr>
		<td style="width:30%">ประเภทลูกค้า</td>
		<td style="width:70%"><?php echo $customer->CUSTYP_TXT?></td>
	</tr>
	<tr>
		<td style="width:30%">ประเภทการเปิดบิล</td>
		<td style="width:70%">
			<?php echo $order->VatType?>
		</td>
	</tr>
	
	<tr>
		<td style="width:30%">ประเภทราคา</td>
		<td style="width:70%"><?php echo $priceType?></td>
	</tr>
	
</table>
<br />

<table cellpadding="6" cellspacing="1"  border="0" class="myTable table2">

<tr>
  <th style="width:30%">สินค้า</th>
  <th style="text-align:right;width:10%">ราคาต่อหน่วย</th>
  <th style="text-align:center;width:10%">หน่วย</th>
  <th style="text-align:center;width:10%">จำนวน</th>
  <th style="text-align:center;width:10%">ราคารวม</th>
  <th style="text-align:center;width:10%">ส่วนลด</th>
  <th style="text-align:center;width:10%">หลังหักส่วนลด</th>
</tr>

<?php $i = 1; ?>
<?
$allDicount = 0;
?>
<?php foreach ($this->cart->contents() as $items): ?>

	<?
	$option = $this -> cart -> product_options($items['rowid']);
	$option = (array)$option;
	$pID = $option['MATNR'];
	$pName = $option['MAKTX'];
	$baseUnit = $option['BASE_UNIT'];
	if ($baseUnit == "KAR") {
		$b = "ลัง";
	} else if ($baseUnit == "PAK") {
		$b = "แพ็ก";
	} else {
		$b = "ชิ้น";
	}
	?>
	<?
	$style = "";
	if (isset($promotion[$items['id']])) {

	}
	?>
	<tr>
	  <td style="width:80%<?php echo $style?>"><?php echo $pID ?><br />
		<?php echo $pName ?></td>
		<td style="text-align:right;width:80px<?php echo $style?>">
	  	<div style="width:80px;float:right;float:right;"><?php echo $this -> cart -> format_number($items['price']); ?></div>
	  </td>
		<td style="width:10%;text-align: center<?php echo $style?>"><?php echo $b?></td>
	 <td style="width:20%;text-align: right;">
			<?php echo converUnit($items, $option); ?></td>
	  
	  <td style="text-align:right;width:80px<?php echo $style?>">
	  	<div style="width:90px;float:right;"><?php echo $this -> cart -> format_number($items['subtotal']); ?></div>
	  </td>
	  <td style="text-align:right;width:80px<?php echo $style?>">
	  	<div style="width:90px;float:right;">
	  		
	  		
	  		<?php 
	  			$discount = 0;
				
				$priceAfterDisc = $items['subtotal'];
	  			if (isset($promoLevel[$items['id']])) {
	  				$discount = getDiscount($promoLevel[$items['id']]);
	  				echo $this -> cart -> format_number($discount);
	  			}else{
	  				echo $this -> cart -> format_number("0");
	  			}
				$allDicount+=$discount;
				$priceAfterDisc = $priceAfterDisc - $discount;
			?>
			<input type="hidden" name="discount[]" value="<?php echo $discount ?>" />
			<input type="hidden" name="priceAfterDisc[]" value="<?php echo $priceAfterDisc ?>" />
	  		</div>
	  </td>
	  <td style="text-align:right;width:80px<?php echo $style?>">
	  	<div style="width:90px;float:right;"><?php echo $this -> cart -> format_number($priceAfterDisc); ?></div>
	  </td>
	</tr>
	<?
	
	if(isset($promotion[$items['id']])){
		
		?>
		
		<? ?>
		
				<?
				foreach($promotion[$items['id']] as $j=>$k){
					
				?>
				<tr class="displaypromo">
			<td colspan="7">
				
					<p><h3 style="font-size:20px"><?php echo $k->PROMO_DESC?></h3></p>
					<p>รายการโปรโมชั่นของ <?php echo $pName?></p>
					<?
					echo displayPromotionFormat($promoLevel[$items['id']]);
					
					$unitAdd = checkUnitAdd($promoLevel[$items['id']]);
					$desc = "";

					foreach ($unitAdd as $s => $l) {
						$desc .= 'data-max-' . $s . '="' . $l['QTY'] . '" ';
					}
					?>
					</td>
			
		</tr>
		<tr class="headdisplaypromo">
			<td colspan="4">
				
					รายการสินค้าแถม
					
					</td>
			<td>
				
				 
				จำนวนที่ได้รับ <?php echo (!isset($unitAdd['KAR'])) ? "0": $unitAdd['KAR']['QTY']?> 
				 <?php echo getUnitDesc("KAR")?>
			</td>
			<td>
				
				จำนวนที่ได้รับ <?php echo (!isset($unitAdd['PAK'])) ? "0": $unitAdd['PAK']['QTY']?> 
				 <?php echo getUnitDesc("PAK")?>
			</td>
			<td>
				จำนวนที่ได้รับ <?php echo (!isset($unitAdd['EA'])) ? "0": $unitAdd['EA']['QTY']?> 
				 <?php echo getUnitDesc("EA")?>
			</td>
		</tr>
		
			<tbody class="bodyinput" data-name="<?php echo $pName ?>" id="<?php echo $items['id']?>" <?php echo $desc?>>
				<?
				foreach($k->promotionList as $a=>$b){
					
				?><tr>
			<td colspan="4">
				
					<p>(<?php echo $b->MATNR?>)
						<?php echo $b->MAKTX?></p>
					
					</td>
			<td>
				<?
				if(isset($unitAdd['KAR'])){
				?>
					<input type="text" style="width:100%" name="addKAR[]" value="0" />
				<?
				}else{
				?>
					<center>0<input align="right" type="hidden" name="addKAR[]" value="0" /></center>
				<?
				}
				?>
				
			</td>
			<td align="right">
				<?
				if(isset($unitAdd['PAK'])){
				?>
					<input align="right" type="text" style="width:100%" name="addPAK[]" value="0" />
				<?
				}else{
				?>
					<center>0</center><input type="hidden" name="addPAK[]" value="0" />
				<?
				}
				?>
			</td>
			<td align="right">
				<?
				if(isset($unitAdd['EA'])){
				?>
					<input align="right" type="text" style="width:100%" name="addEA[]" value="0" />
				<?
				}else{
				?>
					<center>0</center><input type="hidden" name="addEA[]" value="0" />
				<?
				}
				?>
				<input type="hidden" name="premiumID[]" value="<?php echo $b->MATNR?>" />
			</td>
		</tr>
		
				<?
				}
				?>
			</tbody>
				<?
				}
				?>
				
				
			
			
	<?
	}
	?>

<?php $i++; ?>

<?php endforeach; ?>
<tfoot>
<tr>
  <td colspan="4"><strong>ราคารวม</strong></td>
  <td class="right" style="text-align: right"><?php echo $this -> cart -> format_number($this -> cart -> total()); ?></td>
  <td class="right" style="text-align: right"><?php echo $this -> cart -> format_number($allDicount); ?></td>
  <td class="right" style="text-align: right"><?php echo $this -> cart -> format_number($this -> cart -> total()-$allDicount); ?></td>
</tr>
</tfoot>
</table>
<br />
<a href="<?php echo base_url()?>order/editorder/<?php echo $this->session->userdata("CORDERID");?>">กลับไปยังสรุปรายการสั่งซื้อ</a> | 
<button id="mySubmit" type="submit">สรุปรายการสั่งซื้อ</button>
</form>